package com.fc.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {
    @Value("${swaggerShow.show}")
    private Boolean swaggerShow;

    @Bean
    public Docket createRestApi() {
        if (swaggerShow) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.fc.demo.controller"))
                    .paths(PathSelectors.any())
                    .build();
        } else {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("cmo.00.00.0.0"))
                    .paths(PathSelectors.any())
                    .build();
        }


    }
    private ApiInfo apiInfo(){
        if(swaggerShow){
            return new ApiInfoBuilder()
                    .title("springBoot 利用 Swagger 创建 api 文档")
                    .description("测试 swagger2")
                    .version("v1.0")
                    .build();
        }else{
            return new ApiInfoBuilder()
                    .title("项目生产阶段禁止使用 swagger 查看接口")
                    .description("基于 springBoot 的整合开发")
                    .termsOfServiceUrl("http://www.gxaedu.com/")
                    .contact("")
                    .version("")
                    .build();
        }
    }
}
